package structures;
import java.awt.Component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author darin
 *
 */
public class KvComboCellEditor extends AbstractCellEditor
implements TableCellEditor, ActionListener {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox kvCombo;

	/**
	 *
	 * @param data what the list is made from
	 */
	public KvComboCellEditor(final HashMap<Integer, String> data) {
		// create a combo box
		Set<Entry<Integer, String>> entries = data.entrySet();
		Vector<Entry> entryData = new Vector<Entry>();
		Iterator<Entry<Integer, String>> itr = entries.iterator();
		while (itr.hasNext()) {
			entryData.add(itr.next());
		}

		kvCombo = new JComboBox(entryData);
		// give the combobox a custom renderer to handle KvItem
		kvCombo.setRenderer(new KvComboRenderer());
		// listen for events So we know when to stop editing.
		kvCombo.addActionListener(this);
		// make the combobox editable
		kvCombo.setEditable(true);
	}

	@Override
	public final Object getCellEditorValue() {
		return kvCombo.getSelectedItem();
	}

	@Override
	public final Component getTableCellEditorComponent(final JTable table,
			final Object value,
			final boolean isSelected,
			final int row,
			final int column) {
		return kvCombo; // give the combo box to the table
	}

	@Override
	public final void actionPerformed(final ActionEvent e) {
		fireEditingStopped(); // puts cell back into render mode
	}
}
